package com.coolgor.coldot.dao.wechat.oauth2;

import com.coolgor.coldot.entity.wechat.oauth2.OAuth2AccessToken;
import org.apache.ibatis.annotations.*;
import java.util.List;


@Mapper
public interface OAuth2AccessTokenDao {

	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_wechat_oauth2_accesstoken
			WHERE
				record_id = #{recordID}
		</script>
	""")
	@Results(id = "OAuth2AccessTokenMap", value = {
		@Result(property = "recordID", column = "record_id"),
		@Result(property = "appID", column = "app_id"),
		@Result(property = "appName", column = "app_name"),
		@Result(property = "accessToken", column = "access_token"),
		@Result(property = "accessTokenCreateTime", column = "access_token_create_time"),
		@Result(property = "accessTokenInvalidTime", column = "access_token_invalid_time"),
		@Result(property = "accessTokenExpireTime", column = "access_token_expire_time"),
		@Result(property = "refreshToken", column = "refresh_token"),
		@Result(property = "refreshTokenCreateTime", column = "refresh_token_create_time"),
		@Result(property = "refreshTokenInvalidTime", column = "refresh_token_invalid_time"),
		@Result(property = "refreshTokenExpireTime", column = "refresh_token_expire_time"),
		@Result(property = "openid", column = "openid"),
		@Result(property = "scope", column = "scope")
	})
	OAuth2AccessToken queryByID(@Param("recordID") Long recordID);

	@Select("""
		<script>
			SELECT
				*
			FROM
				tb_coldot_wechat_oauth2_accesstoken
			<where>
				<if test="oAuth2AccessTokenCondition.recordID != null">
					and record_id = #{oAuth2AccessTokenCondition.recordID}
				</if>
				<if test="oAuth2AccessTokenCondition.appID != null">
					and app_id like '${oAuth2AccessTokenCondition.appID}'
				</if>
				<if test="oAuth2AccessTokenCondition.appName != null">
					and app_name like '${oAuth2AccessTokenCondition.appName}'
				</if>
				<if test="oAuth2AccessTokenCondition.accessToken != null">
					and access_token like '${oAuth2AccessTokenCondition.accessToken}'
				</if>
				<if test="oAuth2AccessTokenCondition.accessTokenCreateTime != null">
					and access_token_create_time = #{oAuth2AccessTokenCondition.accessTokenCreateTime}
				</if>
				<if test="oAuth2AccessTokenCondition.accessTokenCreateTime != null">
					and <![CDATA[ access_token_create_time >= #{oAuth2AccessTokenCondition.accessTokenCreateTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.accessTokenCreateTime != null">
					and <![CDATA[ access_token_create_time <= #{oAuth2AccessTokenCondition.accessTokenCreateTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.accessTokenInvalidTime != null">
					and access_token_invalid_time = #{oAuth2AccessTokenCondition.accessTokenInvalidTime}
				</if>
				<if test="oAuth2AccessTokenCondition.accessTokenInvalidTime != null">
					and <![CDATA[ access_token_invalid_time >= #{oAuth2AccessTokenCondition.accessTokenInvalidTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.accessTokenInvalidTime != null">
					and <![CDATA[ access_token_invalid_time <= #{oAuth2AccessTokenCondition.accessTokenInvalidTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.accessTokenExpireTime != null">
					and access_token_expire_time = #{oAuth2AccessTokenCondition.accessTokenExpireTime}
				</if>
				<if test="oAuth2AccessTokenCondition.refreshToken != null">
					and refresh_token like '${oAuth2AccessTokenCondition.refreshToken}'
				</if>
				<if test="oAuth2AccessTokenCondition.refreshTokenCreateTime != null">
					and refresh_token_create_time = #{oAuth2AccessTokenCondition.refreshTokenCreateTime}
				</if>
				<if test="oAuth2AccessTokenCondition.refreshTokenCreateTime != null">
					and <![CDATA[ refresh_token_create_time >= #{oAuth2AccessTokenCondition.refreshTokenCreateTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.refreshTokenCreateTime != null">
					and <![CDATA[ refresh_token_create_time <= #{oAuth2AccessTokenCondition.refreshTokenCreateTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.refreshTokenInvalidTime != null">
					and refresh_token_invalid_time = #{oAuth2AccessTokenCondition.refreshTokenInvalidTime}
				</if>
				<if test="oAuth2AccessTokenCondition.refreshTokenInvalidTime != null">
					and <![CDATA[ refresh_token_invalid_time >= #{oAuth2AccessTokenCondition.refreshTokenInvalidTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.refreshTokenInvalidTime != null">
					and <![CDATA[ refresh_token_invalid_time <= #{oAuth2AccessTokenCondition.refreshTokenInvalidTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.refreshTokenExpireTime != null">
					and refresh_token_expire_time = #{oAuth2AccessTokenCondition.refreshTokenExpireTime}
				</if>
				<if test="oAuth2AccessTokenCondition.openid != null">
					and openid like '${oAuth2AccessTokenCondition.openid}'
				</if>
				<if test="oAuth2AccessTokenCondition.scope != null">
					and scope like '${oAuth2AccessTokenCondition.scope}'
				</if>
			</where>
			LIMIT #{rowIndex},#{pageSize};
		</script>
	""")
	@ResultMap("OAuth2AccessTokenMap")
	List<OAuth2AccessToken> queryList(@Param("oAuth2AccessTokenCondition") OAuth2AccessToken oAuth2AccessTokenCondition,
							@Param("rowIndex") int rowIndex,
							@Param("pageSize") int pageSize);

	@Select("""
		<script>
			SELECT
				count(1)
			FROM
				tb_coldot_wechat_oauth2_accesstoken
			<where>
				<if test="oAuth2AccessTokenCondition.recordID != null">
					and record_id = #{oAuth2AccessTokenCondition.recordID}
				</if>
				<if test="oAuth2AccessTokenCondition.appID != null">
					and app_id like '${oAuth2AccessTokenCondition.appID}'
				</if>
				<if test="oAuth2AccessTokenCondition.appName != null">
					and app_name like '${oAuth2AccessTokenCondition.appName}'
				</if>
				<if test="oAuth2AccessTokenCondition.accessToken != null">
					and access_token like '${oAuth2AccessTokenCondition.accessToken}'
				</if>
				<if test="oAuth2AccessTokenCondition.accessTokenCreateTime != null">
					and access_token_create_time = #{oAuth2AccessTokenCondition.accessTokenCreateTime}
				</if>
				<if test="oAuth2AccessTokenCondition.accessTokenCreateTime != null">
					and <![CDATA[ access_token_create_time >= #{oAuth2AccessTokenCondition.accessTokenCreateTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.accessTokenCreateTime != null">
					and <![CDATA[ access_token_create_time <= #{oAuth2AccessTokenCondition.accessTokenCreateTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.accessTokenInvalidTime != null">
					and access_token_invalid_time = #{oAuth2AccessTokenCondition.accessTokenInvalidTime}
				</if>
				<if test="oAuth2AccessTokenCondition.accessTokenInvalidTime != null">
					and <![CDATA[ access_token_invalid_time >= #{oAuth2AccessTokenCondition.accessTokenInvalidTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.accessTokenInvalidTime != null">
					and <![CDATA[ access_token_invalid_time <= #{oAuth2AccessTokenCondition.accessTokenInvalidTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.accessTokenExpireTime != null">
					and access_token_expire_time = #{oAuth2AccessTokenCondition.accessTokenExpireTime}
				</if>
				<if test="oAuth2AccessTokenCondition.refreshToken != null">
					and refresh_token like '${oAuth2AccessTokenCondition.refreshToken}'
				</if>
				<if test="oAuth2AccessTokenCondition.refreshTokenCreateTime != null">
					and refresh_token_create_time = #{oAuth2AccessTokenCondition.refreshTokenCreateTime}
				</if>
				<if test="oAuth2AccessTokenCondition.refreshTokenCreateTime != null">
					and <![CDATA[ refresh_token_create_time >= #{oAuth2AccessTokenCondition.refreshTokenCreateTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.refreshTokenCreateTime != null">
					and <![CDATA[ refresh_token_create_time <= #{oAuth2AccessTokenCondition.refreshTokenCreateTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.refreshTokenInvalidTime != null">
					and refresh_token_invalid_time = #{oAuth2AccessTokenCondition.refreshTokenInvalidTime}
				</if>
				<if test="oAuth2AccessTokenCondition.refreshTokenInvalidTime != null">
					and <![CDATA[ refresh_token_invalid_time >= #{oAuth2AccessTokenCondition.refreshTokenInvalidTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.refreshTokenInvalidTime != null">
					and <![CDATA[ refresh_token_invalid_time <= #{oAuth2AccessTokenCondition.refreshTokenInvalidTime,jdbcType=DATE} ]]>
				</if>
				<if test="oAuth2AccessTokenCondition.refreshTokenExpireTime != null">
					and refresh_token_expire_time = #{oAuth2AccessTokenCondition.refreshTokenExpireTime}
				</if>
				<if test="oAuth2AccessTokenCondition.openid != null">
					and openid like '${oAuth2AccessTokenCondition.openid}'
				</if>
				<if test="oAuth2AccessTokenCondition.scope != null">
					and scope like '${oAuth2AccessTokenCondition.scope}'
				</if>
			</where>
		</script>
	""")
	int queryCount(@Param("oAuth2AccessTokenCondition") OAuth2AccessToken oAuth2AccessTokenCondition);

	@Insert("""
		<script>
			INSERT INTO tb_coldot_wechat_oauth2_accesstoken (
				app_id,
				app_name,
				access_token,
				access_token_create_time,
				access_token_invalid_time,
				access_token_expire_time,
				refresh_token,
				refresh_token_create_time,
				refresh_token_invalid_time,
				refresh_token_expire_time,
				openid,
				scope
			) VALUES (
				#{appID},
				#{appName},
				#{accessToken},
				#{accessTokenCreateTime},
				#{accessTokenInvalidTime},
				#{accessTokenExpireTime},
				#{refreshToken},
				#{refreshTokenCreateTime},
				#{refreshTokenInvalidTime},
				#{refreshTokenExpireTime},
				#{openid},
				#{scope}
			)
		</script>
	""")
	@Options(useGeneratedKeys = true, keyColumn = "record_id", keyProperty = "recordID")
	int insert(OAuth2AccessToken oAuth2AccessToken);

	@Update("""
		<script>
			UPDATE tb_coldot_wechat_oauth2_accesstoken
			<set>
				<if test="appID != null">app_id=#{appID},</if>
				<if test="appName != null">app_name=#{appName},</if>
				<if test="accessToken != null">access_token=#{accessToken},</if>
				<if test="accessTokenCreateTime != null">access_token_create_time=#{accessTokenCreateTime},</if>
				<if test="accessTokenInvalidTime != null">access_token_invalid_time=#{accessTokenInvalidTime},</if>
				<if test="accessTokenExpireTime != null">access_token_expire_time=#{accessTokenExpireTime},</if>
				<if test="refreshToken != null">refresh_token=#{refreshToken},</if>
				<if test="refreshTokenCreateTime != null">refresh_token_create_time=#{refreshTokenCreateTime},</if>
				<if test="refreshTokenInvalidTime != null">refresh_token_invalid_time=#{refreshTokenInvalidTime},</if>
				<if test="refreshTokenExpireTime != null">refresh_token_expire_time=#{refreshTokenExpireTime},</if>
				<if test="openid != null">openid=#{openid},</if>
				<if test="scope != null">scope=#{scope}</if>
			</set>
			WHERE
				record_id = #{recordID}
		</script>
	""")
	int update(OAuth2AccessToken oAuth2AccessToken);

	@Delete("""
		<script>
			DELETE FROM tb_coldot_wechat_oauth2_accesstoken
			WHERE
				record_id = #{recordID}
		</script>
	""")
	int delete(Long recordID);

	@Delete("""
		<script>
			DELETE FROM tb_coldot_wechat_oauth2_accesstoken
			WHERE
				record_id IN
				<foreach collection="list" item="recordID" open="(" separator="," close=")">
					#{recordID}
				</foreach>
		</script>
	""")
	int deleteBatch(List<Long> idList);

}